import Vue from 'vue'
import VueRouter from 'vue-router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'

import App from './App.vue'
import Login from './components/Login.vue'
import Home from './components/Home.vue'

Vue.use(ElementUI)
Vue.use(VueRouter)

const routes = [
    {
        path: '/login', component: Login, meta: {allowAnonymous: true}
        /*beforeEnter: (to, from, next) => {
            document.getElementsByTagName('body')[0].style.backgroundColor = "#94c6dd";
            next();
        }*/
    },
    {
        path: '/home', component: Home
    }
]

const router = new VueRouter({
    routes // （缩写）相当于 routes: routes
})

router.beforeEach((to, from, next) => {
    if (!(to.matched.some(record => record.meta.allowAnonymous || sessionStorage.accessToken))) {
        next({path: '/login', query: {redirect: to.fullPath}})
    } else {
        next()
    }
})

new Vue({
    el: '#app',
    router,
    render: h => h(App)
})
